package com.flying.file;

import java.io.File;
import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * @author 刘鹏飞
 * @ClassName FileTreesTest
 * @Date 2024/12/22 19:42
 * @Created by 刘鹏飞
 * @Description 递归实现目录树的打印
 */
public class FileTreesTest {

    public static void main(String[] args) {

        File file = new File("e:/io");
        boolean exists = file.exists();
//        File file2 = new File("e:/");
        File file1 = new File(System.getProperty("user.dir"));
        printFileTree1(file);
//        printFileTree2(file1,1);
//        printFileTree2(file2,1);


    }

    private static void printFileTree(File file) {
        if (file.isFile()){
            System.out.println("\t\t"+file.getName());  // 如果这个是文件,直接打印文件名,然后停止循环
        }else{
            File[] files = file.listFiles();  // 如果是个目录,则获取目录的全部子目录结构
            for (File file1 : files) {
                if (file1.isDirectory()){
                    System.out.println("--"+file1.getName());
                    printFileTree(file1);
                }else {
                    System.out.println("\t\t"+file1.getName());
                }
            }

        }

    }


    private static void printFileTree1(File file) {
        int level;
         level=file.getParent().split("\\\\").length-1;
        System.out.println("-".repeat(level)+file.getName());

        if (file.isDirectory()){
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    printFileTree1(file1);
                }
            }
        }

    }


    private static void printFileTree2(File file,int level) {

        System.out.println("-".repeat(level)+file.getName());

        if (file.isDirectory()){
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    printFileTree2(file1,level+1);
                }
            }
        }

    }

}
